
/*

  单点应用，进入第三方系统待办列表页后，点击左上角返回按钮时，调 moaBridge.closeWin({{'refresh':true}})
  统一待办进入，点击应用工单，进入第三方系统详情页后，点击左上角返回按钮时，调 moaBridge.backToHome ({{'refresh':true}})
  统一待办进入，点击应用工单，进入第三方系统详情页后，办理完成时调 moaBridge.backToHome ({{'refresh':true}})

    暂无附件预览和下载

*/



var moaBridge = {

    closeWin: function(param){

       if(is_android()){
         var paramJson = JSON.stringify(param)
         window.jsBridge.closeDoc(paramJson);
       }else if(is_iphone() || is_iPad()){
           window.webkit.messageHandlers.closeDoc.postMessage(param);
       }
   },

   backToHome:function(){
       if(is_android()){
         var paramJson = JSON.stringify(param)
         window.jsBridge.closeDoc(paramJson);
       }else if(is_iphone() || is_iPad()){
           window.webkit.messageHandlers.closeDoc.postMessage(param);
       }
   },

   download:function(params){

       var fileName = params.fileName;
       var url = replaceurl(params.url,false);
       
       var uid = params.uid || '';
       var cookie = params.cookie || '';

       if(is_android()){
           window.jsBridge.download(url,fileName,uid,cookie);
       }
       if(is_iphone()){
           window.webkit.messageHandlers.download.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
       }
       if(is_iPad()){
           window.webkit.messageHandlers.download.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
       }

   },

   preview:function(params){

      var fileName = params.fileName;
      var url = replaceurl(params.url,true);
       if(isImg(fileName)){
           url=replaceurl(params.url,false);
       }
      
      var uid = params.uid || '';
      var cookie = params.appId || '';

       if(is_android()){
           window.jsBridge.preview(url,fileName,uid,cookie);
       }
       if(is_iphone()){
           window.webkit.messageHandlers.preview.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
       }
       if(is_iPad()){
           window.webkit.messageHandlers.preview.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
       }

   }

}


var ua = navigator.userAgent.toLowerCase();
//判断环境
function is_android(){
   if(ua.match(/android/i)=="android"&&ua.match(/iemobile/i)!="iemobile"){
        return true;
   } else {
        return false;
   }
}
function is_iphone(){
   if(ua.match(/iphone/i)=="iphone"&&ua.match(/iemobile/i)!="iemobile"){
        return true;
   } else {
        return false;
   }
}
function is_iPad(){
 if(ua.match(/iPad/i)=="iPad"||ua.match(/ipad/i)=="ipad"){
      return true;
 } else {
      return false;
 }
}

function replaceurl(url,type){

   var us=url.split('//');
   
   // let ns = [0x61,0x60,0x7e,0x68,0x67,0x7e,0x64,0x62,0x7e,0x61,0x63,0x66,0x6a,0x68,0x60,0x68,0x68,0x50];
   // let ws = ns;
   let ns = [0x1,0x0,0x1e,0x8,0x7,0x1e,0x1,0x3,0x1e,0x9,0x1,0xa,0x8,0x0,0x8,0x8,0x30];
   let ws = [0x11,0x12,0x12,0xd,0x12,0x10,0x1b,0xd,0x10,0x12,0xd,0x11,0x12,0x13,0x19,0x1b,0x13,0x1b,0x1b,0x23];

   var n = decodeString(ns);
   var w = decodeString(ws);
   
   var uss=us[1].split("/");
   uss.splice(0,1);
   return us[0]+"//"+`${type?n:w}`+'/'+uss.join('/');

}

function decodeString(strs) {
   var size = strs.length;
   var seed = strs[size - 1];
   var decodeStr = '';
   var j = 0;
   do{
       var a = strs[j] ^ seed;
       decodeStr += String.fromCharCode(a);
       j++;
   }while(j < size - 1);
   return decodeStr;
}

//图片文件的后缀名
var imgExt = new Array(".png",".jpg",".jpeg",".bmp",".gif");

//获取文件名后缀名
String.prototype.extension = function(){
   var ext = null;
   var name = this.toLowerCase();
   var i = name.lastIndexOf(".");
   if(i > -1){
   var ext = name.substring(i);
   }
   return ext;
}

//判断Array中是否包含某个值
Array.prototype.contain = function(obj){
   for(var i=0; i<this.length; i++){
       if(this[i] === obj)
           return true;
   }
   return false;
};

//判断是否图片文件
function isImg(filename){
   var ext = filename.extension();
   if(imgExt.contain(ext)){        
       return true;
   }
   return false;
}



window.moaBridge = moaBridge;


